<?php
Zend::loadClass('Lead_EDO_Datatype_Abstract');

/**
 * Datatype for relations
 * 
 * @package EDO
 * @subpackage Datatypes
 *
 */
abstract class Lead_EDO_Datatype_Relation extends Lead_EDO_Datatype_Abstract
{
	
    private $_order;
    
  	private $_className;
  	protected $_foreignKey;
  	
  	
  	private $_resolveObj;
  	
  	/**
  	 * Return the object based on the id - edotype, instead of the relationobject class
  	 * 
  	 * This makes the polymorfic inistation of the objects possible.
  	 * The relation is defined in the superclass, but the instances returned are (possibly)
  	 * instances of the subclass.
  	 * 
  	 * Set this true by defining loose_bind => true in relationconfig
  	 *
  	 * @var boolean
  	 */
  	protected $_looseBind = false;
    
	public function __construct(Lead_EDO_Object_Abstract $owner, $config = array())
	{
		parent::__construct($owner, $config);
		$this->_order = (isset($config['order'])) ? $config['order'] : null;
		$this->_className = $config['class_name'];    
		$this->_looseBind = (isset($config['loose_bind'])) ? $config['loose_bind'] : false;
	
	}
	
	
    public function resolveTags(Lead_EDO_Object_Abstract $resolveObj, $resolve, $nullAsNull = false, $resolver = '_replacer')
    {
        $this->_resolveObj = $resolveObj;
        $this->_nullAsNull = $nullAsNull;
        
        $regexp = "/(\%([a-zA-Z_]*))/si";
        $resolved = preg_replace_callback($regexp, array($this, $resolver), $resolve);
        
        $this->_resolveObj = null;
               
        return $resolved;
    }

	    
    private function _replacer($matches)
    {
        $returner = $this->_resolveObj->getField($matches[2]);
        if(!$returner AND $this->_nullAsNull)
            $returner = 'null';
        
        return $returner;
    }

    private function _replacer2($matches)
    {
        $table = $this->_resolveObj->getFieldsTable($matches[2]);
        
        return $table . '.' . $matches[2];
        
        $returner = $this->_resolveObj->getField($matches[2]);
        if(!$returner AND $this->_nullAsNull)
            $returner = 'null';
        
        return $returner;
    }

    
    public function parseConditionsToTables()
    {
        if($condStr = $this->getConfig('conditions'))
	    {
           $condStr = $this->resolveTags($this->getOwner(), $condStr, false, '_replacer2');
           $condStr = explode(',', $condStr);
	        foreach($condStr as &$cond)
	        {
	            $cond = explode(' ', $cond);
	        }
	        $args = $condStr;
	    
	       return $args;
	    }
        return array();
    }
    
    
    public function parseConditionsToValues()
    {
   	    if($condStr = $this->getConfig('conditions'))
	    {
           $condStr = $this->resolveTags($this->getOwner(), $condStr, true, '_replacer');
           $condStr = explode(',', $condStr);
	        foreach($condStr as &$cond)
	        {
	            $cond = explode(' ', $cond);
	        }
	        $args = $condStr;
	    
	       return $args;
	    }
	    
	    return array();

    }
    
    
    
    public function parseConditions($raw = false)
    {
        
        if($condStr = $this->getConfig('conditions'))
	    {
	        $condStr = explode(',', $condStr);
	        foreach($condStr as &$cond)
	        {
	            $cond = explode(' ', $cond);
	        }
	        $args = $condStr;
  	       return $args;
	    }
	    
	    return array();

    }
    
    
	
	public function &getRelation()
	{
		if(!$this->_relation)
		{
			$this->_relation = $this->getOwner()->getRelation($this->getName());
		}
		return $this->_relation;
	}
	
	
	public function getOrder()
	{
	    return $this->_order;
	}
	
	
	
	
    public function _delete()
    {
        parent::_delete();
        $this->getOwner()->ldb->exec("DELETE FROM edo_dynamic_relations WHERE type_id={$this->getOwner()->getTypeid()} AND fieldname='{$this->getName()}'");

    }
    
    
    public function getClassName()
    {
        return $this->_className;
    }
    
    

    public function validate($validate)
    {
        return true;
    }
    
    public function setValue($value)
    {
        return true;
    }
    
    public function getForeignKey()
    {
        return $this->_foreignKey;
    }
    
	
}
	